const query = require('../../db/query')
const db_user = require('../../db/db_user')
const UserMgr = require('../mgr/UserMgr')
const GroupMgr = require('../mgr/GroupMgr')
class User {
    constructor (ws, uid = 0) {
        this.ws = ws
        this.uid = uid
        this.umgr = UserMgr.i()
        this.groups = new Map() //拥有的群主
        this.friends = []   //拥有的好友
        this.ws.on('message', message => {this.onMessage(JSON.parse(message))})
        this.ws.on('close', close => {this.onClose()})
    }
    bind (uid) {
        this.uid = uid
        this.umgr.add_user(this.uid, this)
        query(db_user.friends, [this.uid])
            .then(result => {
                if (result.length) {
                    for (let item of result) {
                        this.friends.push(item.user_id)
                    }
                }
            }).catch(err => {
            console.log('ERROR:' + err)
        })
        query(db_user.group, [this.uid])
            .then(result => {
                if(result.length) {
                    let group_mgr = GroupMgr.i()
                    for (let item of result) {
                        let group = group_mgr.create_group(item.id)
                        this.groups.push(group)
                    }
                }
            }).catch(err => {
                console.log('ERROR:' + err)
        })
    }
    set_name (name) {
        this.name = name
        return this
    }
    add_group (group) {
        this.groups.set(group.gid, group)
        group.add_user(this)
        return this
    }
    send (msg) {
        if (this.ws) {
            this.ws.send(JSON.stringify(msg))
            return true
        }
        return false
    }

    onMessage (message) {
        console.log(message)
        switch (message.code) {
            case 'login':
                this.bind(message.userid)
                query(db_user.online, [message.uid])
                    .then(result => {
                        // 通知好友在线通知
                        for (let friend in this.friends) {
                            if (this.umgr.has_user(friend)) {
                                let user = this.umgr.get(friend)
                                user.send({ code: 'online', data: { id: this.uid }})
                            }
                        }
                        this.send({ code: 'login', message: '登录成功'})
                    }).catch(err => {
                        this.send({ code: 'error', message: err})
                })
                break;
            case 'primary':
                let friend = message.data.friend
                let msg = message.data.msg
                let user = this.umgr.get_user(friend)
                if (user) {
                    user.send({ code: 'primary', data: {sender: this.uid, msg: msg }})
                }else {
                    this.send({code: 'error', message: '用户不在线'})
                }
        }
    }
    onClose () {
        this.ws = null
        query(db_user.offline, [this.uid])
            .then(result => {
                let mgr = this.umgr
                for(let friend of this.friends) {
                    if (mgr.has_user(friend)) {
                        let user = mgr.get(friend)
                        user.send({ code:'user-off', data: { id: this.uid }})
                    }
                }
                mgr.remove_user(this.uid)
            }).catch(err => {
                console.log('ERROR:' + err)
            })
    }

}

module.exports = User